package com.hh.vietnameseproject.ui.word

import android.app.Activity
import android.app.ActivityOptions
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.hh.vietnameseproject.R
import com.hh.vietnameseproject.logic.model.WordList
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.hh.vietnameseproject.ui.MainActivity
import com.hh.vietnameseproject.ui.changeplan.ChangePlanActivity
import com.hh.vietnameseproject.ui.changeplandetails.PlanDetailsActivity
import com.hh.vietnameseproject.ui.recite.ReciteAtivity
import com.hh.vietnameseproject.ui.search.SearchActivity
import kotlinx.android.synthetic.main.me_fragment.*
import kotlinx.android.synthetic.main.word_fragment.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject
import org.w3c.dom.Text

class WordFragment:Fragment() {

    lateinit var main:MainActivity
    lateinit var wordList:ArrayList<WordList>
    lateinit var dayLearnCount:TextView
    lateinit var playDay:TextView
    var thesaurusId:Int = 1
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.word_fragment,container,false)
        val startLearnBtn = view.findViewById<Button>(R.id.startLearnBtn)
        val changePlan = view.findViewById<TextView>(R.id.changePlanTv)
        val searchEdit = view.findViewById<EditText>(R.id.editTextTextPersonName)
        dayLearnCount = view.findViewById<TextView>(R.id.daylearnCount)
        playDay = view.findViewById<TextView>(R.id.planLearnDay)
        val wordType = view.findViewById<TextView>(R.id.wordType)

        main = activity as MainActivity

        wordList = ArrayList<WordList>()

        startLearnBtn.setOnClickListener {
            val intent = Intent(context,ReciteAtivity::class.java)
            intent.putExtra("id",thesaurusId)
            intent.putExtra("count",dayLearnCount.text.toString().toInt())
            startActivity(intent)
        }
        changePlan.setOnClickListener {
            ChangePlanActivity.actionStart(context,wordList)
        }
        searchEdit.setOnClickListener {
            val intent = Intent(context, SearchActivity::class.java)
            val pair = android.util.Pair<View,String>(searchEdit,"searchEdit")
            val activityOptions = ActivityOptions.makeSceneTransitionAnimation(context as Activity?,pair)
            startActivity(intent,activityOptions.toBundle())
        }


        return view
    }

    override fun onStart() {
        super.onStart()
        wordList.clear()
        GlobalScope.launch {
            val body = AllNetWork.getHeadImage(main.data.id.toString())
            val body1 = JSONObject(AllNetWork.getSchedule().string())
            val body2 = JSONObject(AllNetWork.gettThesaurus().string())
            val b1 = body1.getJSONObject("data")
            val b2 = body2.getJSONArray("data")
            thesaurusId = b1.getInt("thesaurusId")
            var type = ""
            if(body2.getInt("code")==1){
                for (i in 0 until  b2.length()){
                    val a = b2.getJSONObject(i)
                    wordList.add(WordList(a.getInt("id"),a.getString("type"),a.getInt("count"),R.drawable.vitnam))
                    if(thesaurusId==(i+1)){
                        type = a.getString("type")
                    }
                }
            }

            withContext(Dispatchers.Main) {
                dayLearnCount.text = b1.getInt("daylearncount").toString()
                playDay.text = b1.getInt("plandays").toString()
                wordType.text = type
                wordImageView.setImageBitmap(BitmapFactory.decodeStream(body.byteStream()))
            }
        }
    }
}